<?php

namespace App\Http\Controllers\Api\Service;

use App\Models\AdminUsers;
use Illuminate\Support\Facades\Redis;

class LoginService
{



    private static $cache_admin_login_user_key = 'cache_admin_login_user';


    public static function isLogin($request)
    {
        $input_token = $request->header('loginToken') ?? $request->cookie('loginToken');
        try {
            if (empty($input_token)) {
                throw new \Exception('empty token');
            }
            $token = decrypt($input_token);
            if (empty($token)) {
                throw new \Exception('invalidate token');
            }
            $userData = json_decode($token, true);
            $cacheToken = Redis::hget(self::$cache_admin_login_user_key, $userData['id']);
            if ($cacheToken) {
                $result = json_decode($cacheToken, true);
                // if ($result['expire_time'] > time() && $result['token'] == $input_token) {
                //     return true;
                // }
                if ($result['token'] == $input_token) {
                    return true;
                }
            }
            throw new \Exception('未登录');
        } catch (\Exception $e) {
            return false;
        }
    }


    public static function login($data)
    {
        $res = AdminUsers::where(['username' => $data['username']])->first();
        if ($res && decrypt($res['password']) == $data['pass']) {
            $key = $res['id'];
            $value = [
                'expire_time' => time() + 7200,
                'token' => encrypt(json_encode($res))
            ];
            self::setToken($key, $value);
            return $value;
        }
        return false;
    }


    public static function setToken($key, $value)
    {
        Redis::hset(self::$cache_admin_login_user_key, $key, json_encode($value));
    }


    public static function removeToken($key)
    {
        Redis::hdel(self::$cache_admin_login_user_key, $key);
    }

    // public function getToken($key)
    // {
    //     Redis::hgetall($this->cache_admin_login_user_key,$key);
    // }


    public static function getUserInfo($request){
        $input_token = $request->header('loginToken') ?? $request->cookie('loginToken');
        $res = json_decode(decrypt($input_token), true);
        $authService = new AuthService();
        $result = $authService->getMenuByUserId($res['id']);
        $data = array();
        $menu = array();
        $button = array();
        $sonId = array();

        
        //菜单权限
        foreach ($result as $v) {
            if ($v['type'] == 0) {
                $menu[] = $v;
            }
        }
        //按钮权限
        foreach ($result as $v) {
            if ($v['type'] == 1) {
                $button[] = $v;

            }
        }
        

        $data['menu']=getTree($menu,0);
        $data['button']=$authService->getButton($button);
        $data['info']=[
            'id'=>$res['id'],
            'username'=>$res['username'],
            'sex'=>$res['sex'],
            'locked'=>$res['locked'],
        ];
        return $data;
    }

}
